﻿using System;
using System.Collections.Generic;
using System.Text;
using repliback4.Misc.Queue;
using repliback4.Net.Protocol;
using repliback4.XmlStreaming;
using repliback4.Net.Protocol.Messages;
using repliback4.Net.Protocol.States;
using System.Xml;
using Tools.Debugging;

namespace repliback4.Net {

    public enum QueueDirection { In, Out };

    public class SideEndpoint {

        protected QueueManager<InfoQuery, QueueDirection> queue;
        public QueueManager<InfoQuery, QueueDirection> Queue { get { return this.queue; } }
        protected MessageParser parser = new MessageParser( );
        public MessageParser Parser { get { return this.parser; } }
        internal XmlStreamWriter writer;
        protected StateManager stateManager;

        #region :: Parser Event handlers ::

        protected void parser_OnXmlDeclaration ( XmlNode xmlDeclaration ) {
            Logger.Debug( this, "received XML declaration" );
            this.stateManager.CurrentState.ExecuteXmlDeclaration( xmlDeclaration );
        }


        protected void parser_OnStreamEnd ( BaseStream stream ) {
            Logger.Debug( this, "end stream" );
            this.stateManager.CurrentState.ExecuteStreamEnd( stream );

        }

        protected void parser_OnTag ( ReplibackTag stanza ) {
            Logger.Debug( this, "received tag {0}", stanza.GetType( ).FullName );
            try {
                this.stateManager.CurrentState.ExecuteTag( stanza );
            } catch ( Exception e ) {
                Logger.Exception( this, String.Format("parser_OnTag {0}->ExecuteTag: Exception",this.stateManager.CurrentState.GetType().Name), e );
            }
        }

        protected void parser_OnStreamBegin ( BaseStream stream ) {
            Logger.Debug( this, "staring stream {0}", stream.GetType( ).FullName );
            this.stateManager.CurrentState.ExecuteStreamStart( stream );
        }


        protected void parser_OnStreamError ( ) {
            Logger.Debug( this, " stream error{0}" );
            this.stateManager.CurrentState.ExecuteStreamError( );
        }

        #endregion

    }
}
